﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EQual.MainModule.Documents.DocumentsDetailViews
{
    public class DocumentsDestributionPresenter 
    {

        protected DataAccesLayer.EQualDataLayerFactory dataLayer = new DataAccesLayer.EQualDataLayerFactory();

        private Employees .IEmployeesSelectionView  view;

        public Employees .IEmployeesSelectionView  View
        {
            get { return view; }
            set {
                view = value;

                view.ItemAdded += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>>(view_ItemAdded);
                view.ItemDeleted += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>>(view_ItemDeleted);
            }
        }

        void view_ItemDeleted(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object> e)
        {
            RemoveAssociation(e.Value as DataAccesLayer.Employee);
        }

        void view_ItemAdded(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object> e)
        {
            AddNewAssociation(e.Value as DataAccesLayer.Employee);
        }

        public DataAccesLayer.Document ActiveItem { get; set; }

        public DocumentsDestributionPresenter()
        {
            this.View = new Employees.EmployeesSelectionView();
        }
       
        public void DisplayDocumentDestribution(DataAccesLayer .Document doc)
        {
            if (doc == null) return;
            ActiveItem = doc;
            WpfApplicationExperimental.DAOCollection<DataAccesLayer.Employee> empls = new WpfApplicationExperimental.DAOCollection<DataAccesLayer.Employee>(GetEmployees(doc));
            empls.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(empls_CollectionChanged);
            SetUpViewModel(empls);
        }

        protected virtual  void SetUpViewModel(WpfApplicationExperimental.DAOCollection<DataAccesLayer.Employee> empls)
        {
            view.Model = new { ItemsCollection = empls };
        }

        private IEnumerable<DataAccesLayer.Employee> GetEmployees(DataAccesLayer.Document doc)
        {

            IEnumerable<DataAccesLayer.Employee> employees = dataLayer.GetEmployeeAccesLayer().GetEmploeesDocumentNotification(doc.Id.ToString());
            if (employees == null)
                employees = new WpfApplicationExperimental.DAOCollection<DataAccesLayer.Employee>();
            return employees;

        }

        void empls_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
            {
                RemoveAssociation(e.OldItems[0] as DataAccesLayer .Employee );
            }
        }

        private void RemoveAssociation(DataAccesLayer.Employee employee)
        {
            dataLayer.GetEmployeeAccesLayer().RemoveEmployeeToBeNotified(ActiveItem, employee);
        }

        internal void AddNewAssociation(DataAccesLayer.Employee  employee)
        {
            dataLayer.GetEmployeeAccesLayer().AddEmployeeToBeNotified(ActiveItem.Id, employee.id);
        }
    }
}
